package cn.wolfcode.web.controller;

import cn.wolfcode.common.constants.CommonConstants;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.UserUtil;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by lanxw
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInfoController {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IOrderInfoService orderInfoService;
    @RequestMapping("/find")
    public Result<OrderInfo> find(String orderNo,HttpServletRequest request){
        OrderInfo orderInfo=orderInfoService.findByOrderNo(orderNo);
        if (orderInfo==null) {
            return Result.error(CommonCodeMsg.ILLEGAL_OPERATION);
        }
        String token = request.getHeader(CommonConstants.TOKEN_NAME);
        String phone = UserUtil.getUserPhone(redisTemplate, token);

        if (!phone.equals(String.valueOf(orderInfo.getUserId()))) {
            return Result.error(CommonCodeMsg.ILLEGAL_OPERATION);
        }

        return Result.success(orderInfo);
    }

      @RequestMapping("/doSeckill")
      @RequireLogin
    public Result<String> doSeckill(HttpServletRequest request,Integer time, Long seckillId){
        System.out.println("秒杀逻辑");
        SeckillProductVo vo=seckillProductService.find(time,seckillId);
//        boolean legalTime= DateUtil.isLegalTime(vo.getStartDate(),time);
//          if (!legalTime) {
//              return Result.error(CommonCodeMsg.ILLEGAL_OPERATION);
//          }
          String token = request.getHeader(CommonConstants.TOKEN_NAME);
          String phone = UserUtil.getUserPhone(redisTemplate, token);
          String orderSetKey = SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(String.valueOf(seckillId));
          if (redisTemplate.opsForSet().isMember(orderSetKey,phone)) {
              return Result.error(SeckillCodeMsg.REPEAT_SECKILL);
          }
          String seckill_stock_count_hashRealKey = SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(String.valueOf(time));
          Long remainCount = redisTemplate.opsForHash().increment(seckill_stock_count_hashRealKey, String.valueOf(seckillId), -1L);

          if (vo.getCurrentCount()<0) {
              return Result.error(SeckillCodeMsg.SECKILL_STOCK_OVER);
          }
        String orderNo=  orderInfoService.doSeckill(time,seckillId,phone);

          return Result.success(orderNo);
    }

}
